package com.bltec.emergency.controller.resController;

import com.alibaba.fastjson.JSON;
import com.bltec.emergency.common.Constant;
import com.bltec.emergency.common.ResponseJson;
import com.bltec.emergency.model.ResExperts;
import com.bltec.emergency.service.resService.ResExpertsService;
import com.bltec.emergency.service.mgrService.UserService;
import com.bltec.emergency.utils.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/resExperts")
@RequiresPermissions("project:res")
public class ResExpertsController {
    @Resource
    private ResExpertsService resExpertsService;
    @Resource
    private UserService userService;
    /**
     * 获得所有ResExperts
     */
    @RequestMapping("getAllResExperts")
    public ResponseJson getResExperts() {
        try {
            List<ResExperts> resExperts = resExpertsService.getAll();
            return new ResponseJson(Constant.OK, JSON.toJSON(resExperts));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseJson(Constant.FAIL, e.getMessage());
        }
    }

    /**
     * 添加ResExperts
     * id,name,sex,birth,category,type,event_type,department_id,position,tel,create_by,create_time,update_by,update_time,enable
     */
    @RequestMapping("addResExperts")
    @RequiresPermissions("res:insert")
    public ResponseJson addResExperts(String name, String sex, Date birth, Long category, Long type, Long eventType ,Long departmentId,String position ,String tel) {
        try {
            ResExperts resExperts = new ResExperts();
            resExperts.setName(name);
            resExperts.setSex(sex);
            resExperts.setBirth(birth);
            resExperts.setCategory(category);
            resExperts.setType(type);
            resExperts.setEventType(eventType);
            resExperts.setDepartmentId(departmentId);
            resExperts.setPosition(position);
            resExperts.setTel(tel);
            resExperts.setEnable(true);
            resExperts.setCreateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            resExperts.setCreateTime(DateUtil.dateNow());
            resExpertsService.add(resExperts);
            return new ResponseJson(Constant.OK, "创建用户成功");
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, "创建用户失败" + e.getMessage());
        }
    }

    /**
     * 修改ResExperts
     */
    @RequiresPermissions("res:update")
    @RequestMapping("updateResExperts")
    public ResponseJson updateResExperts(Long id ,String name, String sex, Date birth, Long category, Long type, Long eventType ,Long departmentId,String position ,String tel) {
        try {
            ResExperts resExperts = resExpertsService.getResExpertsById(id);
            resExperts.setName(name);
            resExperts.setSex(sex);
            resExperts.setBirth(birth);
            resExperts.setCategory(category);
            resExperts.setType(type);
            resExperts.setEventType(eventType);
            resExperts.setDepartmentId(departmentId);
            resExperts.setPosition(position);
            resExperts.setTel(tel);
            resExperts.setUpdateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            resExperts.setUpdateTime(DateUtil.dateNow());
            resExpertsService.updateResExpertsById(resExperts);
            return new ResponseJson(Constant.OK, JSON.toJSON(resExperts));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseJson.UPDATE_ERROR;
        }
    }

    /**
     * 删除ResExperts
     */
    @RequiresPermissions("res:delete")
    @RequestMapping("deleteResExperts")
    public ResponseJson setDisable(Long id) {
        if (null != resExpertsService.getResExpertsById(id)) {
            if (resExpertsService.deleteResExpertsById(id)) {
                return new ResponseJson(Constant.OK, "删除成功");
            } else {
                return new ResponseJson(Constant.FAIL, "删除失败");
            }
        } else {
            return new ResponseJson(Constant.OK, "删除成功");
        }
    }
}
